//
//  MoreCartoonViewController.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MoreCartoonViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MoreCartoonViewController ()
@property(nonatomic, retain)UITableView *myTableView;
@property(nonatomic, retain)NSMutableArray *moreCartooncArr;
@property(nonatomic, assign)NSInteger page;

//动画
@property(nonatomic, retain)UIView *witeView;
@property(nonatomic, retain)NSMutableArray *witeImageArr;
@property(nonatomic, retain)UIImageView *witeImageView;
@property(nonatomic, retain)UIProgressView *myProgress;
@property(nonatomic, retain)UILabel *myLabel;
@end

@implementation MoreCartoonViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.moreCartooncArr = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createAvtivity];
    [self createWiteImage];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatData];
    [self createView];
    [self createfootView];
    [self.view bringSubviewToFront:self.witeView];
    //播放动画
    [self.witeImageView startAnimating];
}
- (void)createWiteImage{
    self.witeImageArr = [NSMutableArray array];
    NSArray *names = @[@"735d4677ec7cdd522281cfbddd29ac71－1（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－2（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－3（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－4（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－5（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－6（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－7（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－8（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－9（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－10（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－11（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－12（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－13（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－14（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－15（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－16（被拖移）.tiff"];
    
    for (NSString *name in names) {
        UIImage *image = [UIImage imageNamed:name];
        [self.witeImageArr addObject:image];
    }
    self.witeImageView.animationImages = self.witeImageArr;
    //设置播放时间
    self.witeImageView.animationDuration = 0.5;
}
- (void)createAvtivity{
    self.witeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.witeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.witeView];
    [self.witeView release];
    CGFloat bili = 222.0 / 271;
    self.witeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, WIDTH / 1.5 * bili)];
    self.witeImageView.center = CGPointMake(WIDTH / 2, HEIGHT / 3);
    [self.witeView addSubview:self.witeImageView];
    [self.witeImageView release];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, 30)];
    self.myLabel.center = CGPointMake(WIDTH / 2, HEIGHT / 3 + WIDTH / 1.5 * bili / 2 + 10);
    self.myLabel.text = @"loading..........";
    [self.witeView addSubview:self.myLabel];
    self.myLabel.textAlignment = NSTextAlignmentCenter;
    //    self.myProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(100, WIDTH - 50, 200, 30)];
    //    self.myProgress.center = CGPointMake(WIDTH / 2, HEIGHT / 2 - 10);
    //    [self.myProgress setProgressViewStyle:UIProgressViewStyleDefault];
}


- (void)createView{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.myTableView];
    [self.myTableView release];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
}

- (void)createfootView
{
    [self.myTableView addFooterWithCallback:^{
        self.page ++;
        [self creatData];
        [self.myTableView reloadData];
        [self.myTableView footerEndRefreshing];
        
    }];
    
    [self.myTableView addHeaderWithCallback:^{
        NSLog(@"awdawdawdawd");
        self.page = 1;
        [self.moreCartooncArr removeAllObjects];
        [self creatData];
        [self.myTableView reloadData];
        [self.myTableView headerEndRefreshing];
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.moreCartooncArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    ClassifyDetailTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[ClassifyDetailTabelViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    moreOfCartoon *more = self.moreCartooncArr[indexPath.row];
    NSURL *url = [NSURL URLWithString:more.cover_url];
    [cell.myImageView sd_setImageWithURL:url];
    cell.typeLabel.text = more.type;
    cell.titleLabel.text = more.title;
    cell.grade_aveLabel.text = more.grade_ave;
    cell.lastupLabel.text = more.lastup;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CartoonDViewController *cartoonVC = [[CartoonDViewController alloc] init];
    moreOfCartoon *more = self.moreCartooncArr[indexPath.row];
    cartoonVC.comic_id = more.comic_id;
    [self.navigationController pushViewController:cartoonVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat bili1 = 107 / 80.0;
    return WIDTH / 4.5 * bili1 + 20;
}

- (void)creatData{
    
    NSString *moreSTR = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/categoryList/local_version/2.3.2/channel/1001/guest_id/A29A2406-C9BB-4DD1-B7B5-5464AC7A784B/uin/null/category_id/%@/page/%ld/listcnt/12",self.comic_list_id, self.page];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:moreSTR parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *dataArr = dic[@"data"];
        for (NSMutableDictionary *moreDic in dataArr) {
            moreOfCartoon *more = [[moreOfCartoon alloc] init];
            [more setValuesForKeysWithDictionary:moreDic];
            [self.moreCartooncArr addObject:more];
            [more release];
        }
        NSLog(@"%@",self.comic_list_id);
        NSLog(@"COUNT  %ld",self.moreCartooncArr.count);
        NSLog(@"解析数据成功");
        [self.myTableView reloadData];
        NSLog(@"结束动画");
        [self.witeImageView stopAnimating];
        [self.view bringSubviewToFront:self.myTableView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析数据失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
